package com.ufgov.agcfs.baseData.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.servlet.http.HttpServletRequest;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import uap.iweb.entity.DataTable;
import uap.iweb.exception.WebRuntimeException;
import uap.iweb.icontext.IWebViewContext;

import com.ufgov.ip.base.service.IPDataTableService;
import com.ufgov.agcfs.baseData.entity.AccNode;
import com.ufgov.agcfs.baseData.entity.CritAcc;
import com.ufgov.agcfs.baseData.entity.UpdateType;
import com.ufgov.agcfs.baseData.service.CritAccService;
import com.ufgov.agcfs.report.entity.RptTypeNode;

@Controller("baseData.critAcc.critAccController")
@Scope("prototype")
@RequestMapping(value = "/baseData/critAcc")
public class CritAccController {
	private final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private CritAccService critAccService;
	@Autowired 
	IPDataTableService iPDataTableService;
	
	DataTable<CritAcc> accDataTable;
	
	public void loadCritAcc() {
		try {
			Map<String, Object> searchParams = new HashMap<String, Object>();
			searchParams = iPDataTableService.createSearchParamsStartingWith("search_",accDataTable);
			
			List<CritAcc> categoryPage=critAccService.searchCritAcc(searchParams);
			accDataTable.remove(accDataTable.getAllRow());
			accDataTable.set(categoryPage.toArray(new CritAcc[0]));
			IWebViewContext.getResponse().write("true");
		} catch (Exception e) {
			IWebViewContext.getResponse().write("false");
			throw new WebRuntimeException("查询数据失败!");
		}
	}

	
	@RequestMapping(method = RequestMethod.GET,value="getAccList")
	public  @ResponseBody HashMap<String,Object> getAccList(@RequestParam(value="start")int start,
			   @RequestParam(value="length")int length,
			   @RequestParam(value="draw")int draw,HttpServletRequest request){
		try {
			List<CritAcc> accList = critAccService.getAccList();
			int accCnt = accList.size();
			HashMap<String,Object> hashRet = new HashMap<String, Object>();
			hashRet.put("draw", draw);
			hashRet.put("recordsTotal",accCnt);
			hashRet.put("recordsFiltered",accCnt);
			hashRet.put("data",accList);
			return hashRet;
		} catch (Exception e) {
			logger.error(e.getMessage());
			IWebViewContext.getResponse().write("false");
			throw new WebRuntimeException("查询数据失败!");
		}		
	}
	
//	@RequestMapping(method = RequestMethod.GET,value="getAccTree")
//	public @ResponseBody List<AccNode> getAccList(HttpServletRequest request){
//		try {
//			String nd = request.getParameter("nd");
//			if ((nd!=null)&&(!nd.equals(""))){
//				return critAccService.getBookTypeByND(nd);
////			String nd = request.getParameter("nd");
////			if ((nd!=null)&&(!nd.equals(""))){
////				return critAccService.getRptTypeByND(nd);
////			} else
////				return critAccService.getRptTypeNodes();
//		} catch (Exception e) {
//			logger.error(e.getMessage());
//			IWebViewContext.getResponse().write("false");
//			throw new WebRuntimeException("查询数据失败!");
//		}		
//	}

	@RequestMapping(method = RequestMethod.POST,value="saveCritAcc")
	public  @ResponseBody Map<String, String> saveCritAcc(@RequestBody CritAcc critAcc){
		Map<String, String> resultMap = new HashMap<String, String>();
		try {
			critAccService.saveCritAcc(critAcc,resultMap);
			resultMap.put("result", "success");
			resultMap.put("reason", "保存成功！");
		} catch (Exception e) {
			resultMap.put("result", "fail");
			resultMap.put("reason", "保存失败，请稍后重试！");
		}
		return resultMap;
	}
}